

var exceptionIsRoll=null;

var trunoverDuty={//页面总变量
	 //获取周转量和拖期尾数
	 getTrunoverDutyMantissa:function(){
         ajaxUtil.ajaxRuquestNoQuan(baseUrl+'displayView/getTrunoverDutyMantissa',{"token":token,"autoLogin":autoLogin},function(returnData){
        	 console.log(returnData);
        	 var data=returnData.productionList;
        	 var html="";
        	 
        	 var html="<tbody>";
        	 if(returnData.productionList.length==1 && (returnData.putOutList===undefined || returnData.putOutList.length <= 0) && (returnData.purchaseList===undefined || returnData.purchaseList.length<=0)){
        		 html="<thead>";
        	 }
        	 
        	 for(var i=0;i<data.length;i++){
        		 
        		 var sumTrunover=0;//周转量总和
        		 var delayOrderCount=0;//拖期单数
        		 var dutyOrderCount=0;//尾数单数
        		 var workShopThml="";//车间的html
        		 var orderList=data[i].orderList;
        		 for(var j=0;j<orderList.length;j++){
        			 var matiColor='';
        			 var dalyColor='';
        			 var is="否";
        			 if(orderList[j].isMantissa==1){
        				 is="是";
        				 matiColor='red';
        			 }
        			 if(orderList[j].delayTime>0){
        				 dalyColor='red';
        			 }
        			 workShopThml+="<tr>" +
        			 		"<td><span class='titles'>车间：</span><span class='context'>"+data[i].workShopName+"</span></td>" +
        			 		"<td><span class='titles'>单号：</span><span class='context'>"+orderList[j].companyOrderId+"</span></td>" +
        			 		"<td><span class='titles'>厂号：</span><span class='context'>"+orderList[j].factoryId+"</span></td>" +
        			 		"<td><span class='titles'>客号：</span><span class='context'>"+orderList[j].customerId+"</span></td>" +
        			 		"<td><span class='titles'>排期：</span><span class='context'>"+orderList[j].setTime+"</span></td>" +
        			 		"<td><span class='titles'>入库日期：</span><span class='context'>"+orderList[j].inTime.substring(5,10)+"</span></td>" +
        			 		"<td><span class='titles'>入库数：</span><span class='context'>"+orderList[j].inCount+"</span></td>" +
        			 		"<td><span class='titles'>出库数：</span><span class='context'>"+orderList[j].outCount+"</span></td>" +
        			 		"<td><span class='titles'>周转量：</span><span class='context'>"+orderList[j].turnover+"</span></td>" +
        			 		"<td style='color:"+matiColor+"'><span class='titles'>尾数：</span><span class='context'>"+is+"</span></td>" +
        			 		"<td style='color:"+dalyColor+"'><span class='titles'>拖期：</span><span class='context'>"+orderList[j].delayTime+"</span></td>" +
        			 		"</tr>";
        			 sumTrunover=sumTrunover+orderList[j].turnover;//总周转量
        			 if(orderList[j].delayTime >0 ){
        				 delayOrderCount++;//拖期
        			 }
        			 if(orderList[j].isMantissa==1){
        				 dutyOrderCount++;//尾数
        			 }
        		 }
        		 var workshopTitle=`<tr style='background-color:#FFEFD5;'><td colspan='11' >车间名称：${data[i].workShopName} &nbsp;&nbsp;&nbsp; 周转量：${sumTrunover} &nbsp;&nbsp;&nbsp;  尾数单数：${dutyOrderCount} &nbsp;&nbsp;&nbsp;  拖期单数：${delayOrderCount}</td></tr>`;
        		 if(returnData.productionList.length==1 && (returnData.putOutList===undefined || returnData.putOutList.length <= 0) && (returnData.purchaseList===undefined || returnData.purchaseList.length<=0)){
        			 workshopTitle=`<tr style='background-color:#FFEFD5;'><td colspan='11' >车间名称：${data[i].workShopName} &nbsp;&nbsp;&nbsp; 周转量：${sumTrunover} &nbsp;&nbsp;&nbsp;  尾数单数：${dutyOrderCount} &nbsp;&nbsp;&nbsp;  拖期单数：${delayOrderCount}</td></tr></thead><tbody>`;
            	 }
        		 html+=workshopTitle;
        		 html+=workShopThml;
        	 }
        	 var  putOutHtml="";
        	 //有查看外协权限
        	 if(returnData.putOutList!==undefined){
        		 var putOutList=returnData.putOutList;
//        		 var putOutHtml="<tr style='background-color:#F0F8FF;'><td>类型</td><td>订单编号</td><td>工厂型号</td><td>客户型号</td><td>供应商</td><td>制单时间</td><td>流程周期</td><td>外协数量</td><td>返回数量</td><td>欠数</td><td>拖期天数</td></tr>";
        		 var putOutHtml="";
        	     var sumCount=0.0;
        		 for(var i=0;i<putOutList.length;i++){
        	    	 var chaCount=putOutList[i].count-putOutList[i].returnCount;
        	    	 sumCount=sumCount+chaCount;
        	    	 putOutHtml+="<tr>" +
        	    	 		         "<td><span class='titles'>类型：</span><span class='context'>外协</span></td>" +
        	    	 		         "<td><span class='titles'>单号：</span><span class='context'>"+putOutList[i].companyOrderId+"</span></td>" +
        	    	 		         "<td><span class='titles'>厂号：</span><span class='context'>"+putOutList[i].factoryId+"</span></td>" +
        	    	 		         "<td><span class='titles'>客号：</span><span class='context'>"+putOutList[i].coustomId+"</span></td>" +
        	    	 		         "<td><span class='titles'>供应商：</span><span class='context'>"+putOutList[i].supplierName+"</span></td>" +
        	    	 		         "<td><span class='titles'>建期：</span><span class='context'>"+putOutList[i].createTime.substring(5,11)+"</span></td>" +
        	    	 		         "<td><span class='titles'>周期：</span><span class='context'>"+putOutList[i].flowCycle+"</span></td>" +
        	    	 		         "<td><span class='titles'>数量：</span><span class='context'>"+putOutList[i].count+"</span></td>" +
        	    	 		         "<td><span class='titles'>返回数量：</span><span class='context'>"+putOutList[i].returnCount+"</span></td>" +
        	    	 		         "<td style='color:red'><span class='titles'>欠数：</span><span class='context'>"+chaCount+"</span></td>" +
        	    	 		         "<td style='color:red'><span class='titles'>拖期：</span><span class='context'>"+putOutList[i].delayTime+"</span></td>" +
        	    	 		     "</tr>";
        	     }
        	     if(putOutList.length>0){
        	    	 html+="<tr style='background-color:#FFEFD5;'><th colspan='11' >类型：外协     &nbsp;&nbsp;&nbsp;       未返回数："+sumCount+" &nbsp;&nbsp;&nbsp;    拖期单数："+putOutList.length+"</th></tr>";
        	    	 html+=putOutHtml;
        	     }
        	 }
        	 
        	 
        	 var  purchaseHtml="";
        	 //有查看采购权限
        	 if(returnData.purchaseList!==undefined){
        		 var purchaseList=returnData.purchaseList;
//        		 var purchaseHtml="<tr style='background-color:#F0F8FF;'><td>类型</td><td>订单编号</td><td>工厂型号</td><td>客户型号</td><td>制单时间</td><td>最大复期</td><td>采购数量</td><td>入库数量</td><td>欠数</td><td>拖期天数</td><td>采购人</td></tr>";
        		 var purchaseHtml="";
        		 var sumCount=0.0;
        		 
        		 for(var i=0;i<purchaseList.length;i++){
        			 var laterPhase="";
            		 if(purchaseList[i].laterPhaseFour != '' && purchaseList[i].laterPhaseFour != null){
            			 laterPhase=purchaseList[i].laterPhaseFour;
            		 }else if(purchaseList[i].laterPhaseThr != '' && purchaseList[i].laterPhaseThr != null){
            			 laterPhase=purchaseList[i].laterPhaseThr;
            		 }else if(purchaseList[i].laterPhaseTwo != '' && purchaseList[i].laterPhaseTwo != null){
            			 laterPhase=purchaseList[i].laterPhaseTwo;
            		 }else if(purchaseList[i].laterPhaseOne != '' && purchaseList[i].laterPhaseOne != null){
            			 laterPhase=purchaseList[i].laterPhaseOne;
            		 }
            		 if(laterPhase!=''){
            			 laterPhase=laterPhase.substring(0,11);
            		 }
        			 var chaCount=purchaseList[i].purchaseCount-purchaseList[i].inCount;
        			 sumCount=numAdd(sumCount,chaCount);
//        			 var startTime= new Date(Date.parse(laterPhase));
//        			 var endTime= new Date();
//        			 var delayTime=parseInt((endTime.getTime() - startTime.getTime()) / (1000 * 60 * 60 * 24));
        			 
        			 purchaseHtml+="<tr>" +
        			 "<td><span class='titles'>类型：</span><span class='context'>采购</span></td>" +
        			 "<td><span class='titles'>单号：</span><span class='context'>"+purchaseList[i].companyOrderId+"</span></td>" +
        			 "<td><span class='titles'>厂号：</span><span class='context'>"+purchaseList[i].factoryNo+"</span></td>" +
        			 "<td><span class='titles'>客号：</span><span class='context'>"+purchaseList[i].articleNo+"</span></td>" +
//        			 "<td>"+purchaseList[i].providerName+"</td>" +
        			 "<td><span class='titles'>建期：</span><span class='context'>"+purchaseList[i].createTime.substring(0,11)+"</span></td>" +
        			 "<td><span class='titles'>复期：</span><span class='context'>"+laterPhase+"</span></td>" +
        			 "<td><span class='titles'>采购数：</span><span class='context'>"+purchaseList[i].purchaseCount+"</span></td>" +
        			 "<td><span class='titles'>入库数：</span><span class='context'>"+purchaseList[i].inCount+"</span></td>" +
        			 "<td style='color:red'><span class='titles'>欠数：</span><span class='context'>"+chaCount+"</span></td>" +
        			 "<td style='color:red'><span class='titles'>拖期：</span><span class='context'>"+(-purchaseList[i].tardiness)+"</span></td>" +
        			 "<td><span class='titles'>采购人：</span><span class='context'>"+purchaseList[i].purchasePeople+"</span></td>" +
        			 "</tr>";
        		 }
        		 if(purchaseList.length>0){
        			 sumCount=sumCount+chaCount;
        			 html+="<tr style='background-color:#FFEFD5;'><th colspan='11' >类型：采购     &nbsp;&nbsp;&nbsp;       欠数："+sumCount+" &nbsp;&nbsp;&nbsp;    拖期单数："+purchaseList.length+"</th></tr>";
        			 html+=purchaseHtml;
        		 }
        	 }
        	 html+="</tbody>";
        	 $("#xx").html(html);
        	 
        	 trunoverDuty.rollTable();
        	 page.rollTable();
		 });
	 },
	 rollTable:function(){
			//已经在滚动了,就不再滚动
			var displayHeight=0;
			var bodyHeight=0;
			displayHeight=$(window).height();
			bodyHeight=$("body").height();
			//如果表格高度大于div的高度，则需要轮播
			if(bodyHeight>displayHeight){
				if(exceptionIsRoll!=null){
					return false;
				}
				exceptionIsRoll=setInterval("trunoverDuty.tableScroll()", 4000);
			}else{
				if(exceptionIsRoll!=null){
					clearInterval(exceptionIsRoll);
					exceptionIsRoll=null;
				}
			}
			return true;
		},
		tableScroll:function(){
			$("#xx tbody").append($("#xx tbody tr:first"));
		}
}


function numAdd(num1, num2) {
	 var baseNum, baseNum1, baseNum2;
	 try {
	  baseNum1 = num1.toString().split(".")[1].length;
	 } catch (e) {
	  baseNum1 = 0;
	 }
	 try {
	  baseNum2 = num2.toString().split(".")[1].length;
	 } catch (e) {
	  baseNum2 = 0;
	 }
	 baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
	 return (num1 * baseNum + num2 * baseNum) / baseNum;
	};